package cn.com.czpb.client.module.news.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import java.util.List;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.HtmlActivity;
import cn.com.czpb.client.model.news.FuWuItemBean;
import cn.com.czpb.client.model.news.FuWuMainBean;
import cn.com.czpb.client.module.news.view.MyGridViewForFuwu;
import cn.com.czpb.client.utils.LogUtils;

/**
 * Created by Administrator on 2018/2/24.
 */

public class FuwuMainAdapter extends RecyclerView.Adapter<FuwuMainAdapter.ViewHolder>{
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    private List<FuWuMainBean> data;
    private Context context;
    public FuwuMainAdapter(List<FuWuMainBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

//    @Override
//    protected void convert(BaseViewHolder holder, FuWuMainBean item) {
//        holder.setText(R.id.item_fuwu_main_name,item.getName());
//        FuWuMainItemAdapter adapter = new FuWuMainItemAdapter(item.getServer_list(),mContext);
//        holder.setAdapter(R.id.item_fuwu_main_list,adapter);
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fuwu_main, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTv;
        MyGridViewForFuwu gridview;
        public ViewHolder(View itemView) {
            super(itemView);
            mTv = itemView.findViewById(R.id.item_fuwu_main_name);
            gridview = itemView.findViewById(R.id.item_fuwu_main_list);
            gridview.setVisibility(View.GONE);
            gridview.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTv.setText(data.get(position).getName());
        if(data.get(position).getServer_list() != null) {
            FuWuMainItemAdapter adapter = new FuWuMainItemAdapter(data.get(position).getServer_list(), context);
            final List<FuWuItemBean> list = data.get(position).getServer_list();
            holder.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                    Intent intent = new Intent();
                    intent.setClass(context, HtmlActivity.class);
                    intent.putExtra("url",list.get(position2).getLink());
                    context.startActivity(intent);
                    LogUtils.e("fuwu-url:"+list.get(position2).getLink());
                }
            });
            holder.gridview.setAdapter(adapter);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
